<?php
include_once dirname(__FILE__).'/../_config.php';

/**
 * 在这里定义本目录下的特定变量
 */

// 管理员信息
$__oManager = new Manager();
$__s_manager_self		= $__oManager->get_session_cookie();
$__f_manager_info_cur	= null;
if(empty($__s_manager_self)){
	$__setting_syncjs = '';
}else{
	#$__userid		= $__s_manager_self['userid'];
	#$__username		= $__s_manager_self['username'];
	#$__role			= $__s_manager_self['role'];
	#$__rolename		= $__s_manager_self['rolename'];
	#$__super		= $__s_manager_self['super'];
	
	VS::set('managerinfo', $__s_manager_self);
	VS::update($__s_manager_self);
	
	// 保存设置到COOKIE
	$__setting_syncjs = $__oManager->set_setting_cookie();
}

$__f_styles = cache_read('styles.php');
$__f_langs = cache_read('langs.php');

if(!empty($__userid) && !empty($__role)){

	$_role_info = cache_read('role_'.$__role.'.php');
	$__f_manager_info_cur = cache_read('manager_'.$__s_manager_self['id'].'.php');
	$__f_password = $__f_manager_info_cur['info']['password'];	// 用于密码被修改后，强制重新登录用
	array_update($__f_manager_info_cur['info'], $__s_manager_self);

	/********************************************************************************
	 * 语言控制、样式控制，此设置缓存在管理员的缓存文件中
	 ********************************************************************************/
	
	if(isset($__f_manager_info_cur['setting']) || !empty($__f_manager_info_cur['setting'])){
		$__f_style_setting	= $__f_styles[$__f_manager_info_cur['setting']['style']];
		$__f_lang_setting	= $__f_langs['lang_mark_list'][$__f_manager_info_cur['setting']['language']];

		if($__f_manager_info_cur['info']['debugging'] == 1){
			if($__f_manager_info_cur['setting']['debug_pr'] == 1){
				define('DEBUG_PR', true);
			}

			if($__f_manager_info_cur['setting']['debug_fb'] == 1){
				define('DEBUG_FB', true);
			}
		}
	}

	// ...
}

// 模板样式文件路径处理
if(empty($__f_style_setting))$__f_style_setting = $__f_styles['default'];				// 未设置样式用户采用默认样式，由系统控制
if(empty($__f_lang_setting))$__f_lang_setting = $__f_langs['lang_mark_list']['zh-cn'];	// 未设置语言，默认为中文

$__theme_pth = THR_PTH.'_manager/_skin/'.$__f_style_setting['_style'].'/';
$__theme_dir = THR_DIR.'_manager/_skin/'.$__f_style_setting['_style'].'/';

$__lang_pth = LNG_PTH.$__f_lang_setting['mark'].'/';
$__lang_dir = LNG_DIR.$__f_lang_setting['mark']."/";

$__tpl_pth = THR_PTH.'_manager/';
$__tpl_dir = THR_DIR.'_manager/';

// 加载通用语言包及与当前模块相关的语言包
$__modules_arr = array('gm_common', 'gm_menu');
if($__frm != ''){
	$_f_librarys = cache_read('librarys.php');
	if(array_key_exists($__frm, $_f_librarys['library_mark_list'])){
		$__modules_arr[] = "gm_{$__frm}";
	}
}
$__l = ea_read_language($__f_lang_setting, $__modules_arr);
include_once dirname(__FILE__).'/lock.php';

/*******************************************************************************
 * 全局变量
 *******************************************************************************/

//include_once CNF_PTH.'static.inc.php';	## 已换用Statics.class.php

$GLOBALS['runtime']['power'] = microtime(true);

// 一些默认参数
$_pagesize = get_session('pagesize');
$__pagesize = empty($_GET['pagesize']) ? $_pagesize : intval($_GET['pagesize']);
if(empty($__pagesize))$__pagesize = PAGE_SIZE;
$_GET['pagesize'] = $__pagesize;	// 每页记录数
if($_pagesize != $__pagesize)set_session('pagesize', $__pagesize);
$__page = isset($_GET['page']) ? max(intval($_GET['page']), 1) : 1;
$_GET['isdel'] = empty($_GET['isdel']) ? 0 : intval($_GET['isdel']);

if(!empty($__userid) && !empty($__role)){
	// 运营商标识
	$identifier = @$_role_info['setting']['identifier'];
	$identifier = empty($identifier) ? 'DEFAULT' : $identifier;		// 未设置则为官方
}

// if((defined('DEBUG_PR') && DEBUG_PR == true) || (defined('DEBUG_FB') && DEBUG_FB == true)){
// 	// 调试类
// 	##@include_once CLS_PTH.'tools/Trace.class.php';
// }

if(defined('DEBUG_PR') && DEBUG_PR == true){
	$GLOBALS['debug_info'][] = anchor('request');
	$GLOBALS['debug_info']['REQUEST'] = array('GET' => $_GET, 'POST' => $_POST);
	$GLOBALS['debug_info']['mark'] = $identifier;
	
	if(isset($_GET['manager']) && $_GET['manager'] == 'yes'){
		$GLOBALS['debug_info']['manager'] = array(anchor('manager'), '_manager' => $__s_manager_self, '$__f_manager_info_cur' => $__f_manager_info_cur, '_role_info' => $_role_info);
	}
}

if(defined('DEBUG_FB') && DEBUG_FB == true){
	// 加载 FirePHP 调试服务端代码
	include_once LIB_PTH.'FirePHPCore/fb.php';

	FB::trace('_config.php');
	fb(array('GET' => $_GET, 'POST' => $_POST), 'REQUEST');
	fb($identifier, 'mark');
	if(isset($_GET['funcs']) && $_GET['funcs'] == 'yes'){
		fb(Trace::get_user_funcs(), 'funcs');
	}

	if(isset($_GET['roleinfo']) && $_GET['roleinfo'] == 'yes'){
		fb(array('_manager' => $__s_manager_self, '$__f_manager_info_cur' => $__f_manager_info_cur, '_role_info' => $_role_info), 'roleinfo');
	}
}

/*******************************************************************************
 * 系统信息
 *******************************************************************************/
function sysinfo($msg, $url = '', $ttl = 2000){
	global $vars, $__tpl, $__tpl_pth, $__tpl_dir, $__l, $__setting_syncjs, $__lang_dir, $__theme_dir, $__f_style_setting, $_role_info, $__s_manager_self, $__f_manager_info_cur, $__f_lang_setting, $__f_style_setting, $__frm, $__do, $__mdl, $__modules_arr;
	global $__f_econfig, $__f_sysvars;

	$vars['uri'] = array('msg' => $msg,'url' => $url, 'ttl' => $ttl, 'second' => ceil($ttl / 1000));
	if(!empty($url)){
		switch ($url){
			case 'back2edit':	// 内容页编辑完成后，默认返回内容页
				$vars['uri']['type'] = 'back2edit';
				break;
			default:			// 其他情况，直接按地址跳转
				$vars['uri']['type'] = 'transfer';
		}
	}
	
	$referer = trim(REF_URL);
	if(empty($referer)){
		$referer = './m.php';
	}else{
		$referer = str_replace(WEB_URL, './', $referer);
	}
	$vars['uri']['referer'] = $referer;
	
	$__tpl['path'] = '';
	$__tpl['body'] = 'sysinfo.html';
	include_once dirname(__FILE__).'/_flush.php';
	exit;
}

/********************************************************************************
 * 消息
 ********************************************************************************/

function ajax_callback($result, $msg, $callback = 'callback', $synjs = '', $url ='', $ttl = 5000, $target = 'list'){
	$msg = addslashes($msg);
	$jsdir = JS_DIR;
	$cookie_pre = COOKIE_PRE;
	if($target == 'list'){
		$target = 'top.frames["iBody"]';
	}else{
		$target = 'window.parent';
	}
	echo $thml = <<<HTML
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>AJAX</title>

		<script type="text/javascript" language="JavaScript" charset="utf-8" src="{$jsdir}func.js"></script>
		<script type="text/javascript" language="JavaScript" charset="utf-8">
		<!--
			var _s = {_cookie_pre:'{$cookie_pre}'};
		//-->
		</script>
		{$synjs}
		<script type="text/javascript" language="JavaScript" charset="utf-8">
		<!--
			　　var params = {};
			　　params['result']= '{$result}';
			　　params['msg']= '{$msg}';
			　　params['url']= '{$url}';
			　　params['ttl']= '{$ttl}';
			try{
				//setTimeout(function(){
					{$target}.{$callback}(params);
				//}, {$ttl});
			}catch(e){}
		//-->
		</script>

	</head>
	<body>
		
		<!-- 这里是伪Ajax输出，您能看到这个，说明程序没有语法错误 -->
		这里是伪Ajax输出，您能看到这个，说明程序没有语法错误

	</body>
</html>
HTML
;
	if(defined('DEBUG_PR') && DEBUG_PR == true){
		if(isset($GLOBALS['debug_info'])){
			echo "<pre>\n";
			echo anchor('debug_info', '调试信息');
			print_r($GLOBALS['debug_info']);
			echo "</pre>\n";
		}
	}
	exit;
}

// 列表页-弹出消息
/*function alert_msg($msg, $ref = ''){
	$url = empty($ref) ? "top.frames['iBody'].location" : "'{$ref}'";
	$jsdoc = <<<JSDOC
<script type='text/javascript'>
	top.frames['iBody'].alert('{$msg}');
	setTimeout(function(){
		top.frames['topnav'].eori();
		top.frames['iBody'].location = {$url};
	}, 1000);
</script>
JSDOC;
	echo $jsdoc;
	exit;
}*/

function alert_top($msg, $url = ''){
	$url = empty($url) ? './m.php' : $url;
	$jsdoc = <<<JSDOC
<script type='text/javascript'>
	top.frames['iBody'].alert('$msg');
	top.location = '$url';
</script>
JSDOC;
	echo $jsdoc;
	exit;
}

/********************************************************************************
 * 列表相关操作函数
 ********************************************************************************/

// 主键排序
function make_major_key_options($major_key_arr = array()){
	global $vars;
	$_GET['major_key'] = empty($_GET['major_key']) ? '' : trim($_GET['major_key']);
	$_GET['order_dir'] = empty($_GET['order_dir']) ? '' : trim($_GET['order_dir']);
	$vars['major_key_options'] = make_options($major_key_arr, true, $_GET['major_key']);
	$vars['order_dir_options'] = make_options(Statics::$order_dir_arr, true, $_GET['order_dir']);
}

/**
 * 读取排序项目
 * @param  array  $orderby 默认排序项目
 * @param  string  $need_pre    前置必需项
 * @param  string  $need_suf    后置必需项
 * @return array           排序项目
 */
function get_major_key($orderby = array(), $need_pre = '', $need_suf = ''){
	$_GET['major_key'] = empty($_GET['major_key']) ? '' : trim($_GET['major_key']);
	if($_GET['major_key'] == ''){
		$_orderby = $orderby;
	}else{
		$_orderby = array();
		if(!empty($need_pre)){
			$need_pre = explode(',', $need_pre);
			foreach($need_pre as $field){
				$_orderby["{$field}"] = $orderby["{$field}"];
			}
		}
		$_GET['order_dir'] = empty($_GET['order_dir']) ? 'DESC' : trim($_GET['order_dir']);
		$_orderby[$_GET['major_key']] = $_GET['order_dir'];
		if(!empty($need_suf)){
			$need_suf = explode(',', $need_suf);
			foreach($need_suf as $field){
				$_orderby["{$field}"] = $orderby["{$field}"];
			}
		}
	}
	return $_orderby;
}

/********************************************************************************
 * 格式化导航菜单树
 ********************************************************************************/

function make_role_ul($menu_tree, $menus = array(), $powers = array()){
	global $__l;	// 语言包
	$menu_arr = array();
	foreach($menu_tree as $menu){
		$power =  unserialize($menu['power']);
		if(!empty($power)){
			foreach($power as $key => $val){
				$power[$val] = $__l[$val];
				unset($power[$key]);
			}
		}
		if(in_array($menu['id'], $menus)){
			$checked = 'checked';
			$__powers = make_checkbox_group('powers', $power, $powers);
		}else{
			$checked = '';
			$__powers = make_checkbox_group('powers', $power);
		}
		
		$menu_arr[] = '<li><div class="menu"><label title="'.$menu['name'].'" class="poshytip"><input type="checkbox" name="menus[]" value="'.$menu['id'].'" '.$checked.' class="chbox" />'.$__l[$menu['name']].'</label></div><div class="power">'.$__powers.'</div><div class="clear"></div>';
		if(isset($menu['menus']) && is_array($menu['menus'])){
			$menu_arr[] = '<ul>'.make_role_ul($menu['menus'], $menus, $powers).'</ul>';
		}
		$menu_arr[] = '</li><div class="clear"></div>';
	}
	return implode("\n", $menu_arr);
}
?>